package com.twu.biblioteca.Controller;

import com.twu.biblioteca.Domain.BookService;
import com.twu.biblioteca.Domain.IOService;
import com.twu.biblioteca.Domain.MenuService;
import com.twu.biblioteca.Domain.MovieService;
import com.twu.biblioteca.Domain.RecordService;
import com.twu.biblioteca.Domain.UserService;

/**
 * Created by hjli on 9/9/14.
 */
public class BibliotecaController {


    public BookService getBookService() {
        return bookService;
    }

    private BookService bookService;

    public MovieService getMovieService() {
        return movieService;
    }

    private MovieService movieService;

    private UserService userService;

    private MenuService menuService;

    private RecordService recordService;

    private IOService ioService;


    public BibliotecaController(BookService bookService, MovieService movieService, UserService userService, MenuService menuService, RecordService recordService, IOService ioService) {
        this.bookService = bookService;
        this.movieService = movieService;
        this.userService = userService;
        this.menuService = menuService;
        this.recordService = recordService;
        this.ioService = ioService;
    }

    public BibliotecaController(){
        bookService = new BookService();
        movieService = new MovieService();
        userService = new UserService();
        movieService = new MovieService();
        recordService = new RecordService();
        ioService = new IOService();
        menuService = new MenuService();
    }

    public void showWelcomeMessage(){
        ioService.write("Welcome.");
    }

    public void start(){
        try {
            showWelcomeMessage();
            menuService.setMenuItems(menuService.getUnLoginMainMenuItems());

            while (menuService.menuItems != null){
                enterMenu();
            }
        } catch (Exception e) {
            ioService.write("Bye.");
            Thread.currentThread().interrupt();
        }

    }

    private void enterMenu() throws Exception {
        ioService.write(menuService.showMainMenu());
        String input = ioService.read();
        String result = menuService.clickMenu(input);
        while (result.equals("Select a valid option!")){
            System.out.println(result);
            input = ioService.read();
            result = menuService.clickMenu(input);
        }
        processInput(result);
    }

    public void processInput(String option) throws Exception {

        if (option.equals("List Books")) {
            enterListBooks();
        }
        else if(option.equals("List Movies")){
            enterListMovies();
        }
        else if(option.equals("Login In")){
            enterLoginIn();
        }
        else if(option.equals("Login Out")){
            enterLoginOut();
        }
        else if(option.equals("Personal Info")){
            enterPersonalInfo();
        }
        else if(option.equals("List Record")){
            enterListRecord();
        }
        else if (option.equals("Checkout Book")) {
            enterCheckoutBook();
        }
        else if (option.equals("Return Book")) {
            enterReturnBook();
        }
        else if (option.equals("Checkout Movie")) {
            enterCheckoutMovie();
        }
        else if (option.equals("Return Movie")) {
            enterReturnMovie();
        }
    }

    private void enterPersonalInfo() {
        ioService.write(userService.showPersonalInfo());
    }

    private void enterReturnMovie() throws Exception {
        String input;
        ioService.write("please input the id of Movie to return:");
        input = ioService.read();
        ioService.write(movieService.returnTo(input));
        if(userService.getCurrentUser().getRole().equals("librarian")){
            menuService.setMenuItems(menuService.getLibrarianMainMenuItems());
        }
        else{
            menuService.setMenuItems(menuService.getCustomerMainMenuItems());
        }
        recordService.remove(input);
    }

    private void enterCheckoutMovie() throws Exception {
        String input;
        ioService.write("please input the id of Movie to checkout:");
        input = ioService.read();
        ioService.write(movieService.checkout(input));
        if(userService.getCurrentUser().getRole().equals("librarian")){
            menuService.setMenuItems(menuService.getLibrarianMainMenuItems());
        }
        else{
            menuService.setMenuItems(menuService.getCustomerMainMenuItems());
        }
        recordService.add(input,userService.getCurrentUser().toString());
    }

    private void enterReturnBook() throws Exception {
        String input;
        ioService.write("please input the id of Book to return:");
        input = ioService.read();
        System.out.println(bookService.returnTo(input));
        if(userService.getCurrentUser().getRole().equals("librarian")){
            menuService.setMenuItems(menuService.getLibrarianMainMenuItems());
        }
        else{
            menuService.setMenuItems(menuService.getCustomerMainMenuItems());
        }
        recordService.remove(input);
    }

    private void enterCheckoutBook() throws Exception {
        String input;
        ioService.write("please input the id of Book to checkout:");
        input = ioService.read();
        System.out.println(bookService.checkout(input));
        recordService.add(input, userService.getCurrentUser().toString());
        if(userService.getCurrentUser().getRole().equals("librarian")){
            menuService.setMenuItems(menuService.getLibrarianMainMenuItems());
        }
        else{
            menuService.setMenuItems(menuService.getCustomerMainMenuItems());
        }
        recordService.add(input, userService.getCurrentUser().toString());
    }

    private void enterListRecord() {
        System.out.println(recordService.listRecords());
    }

    private void enterLoginOut() {
        userService.setCurrentUser(null);
        menuService.setMenuItems(menuService.getUnLoginMainMenuItems());
    }

    private void enterLoginIn() throws Exception {
        String libraryNumber, password;
        ioService.write("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& sign in &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        ioService.write("please input your library number");
        libraryNumber = ioService.read();
        ioService.write("please input your password:");
        password = ioService.read();
        userService.logIn(libraryNumber, password);
        if(userService.getCurrentUser().getRole().equals("librarian")){
            menuService.setMenuItems(menuService.getLibrarianMainMenuItems());
        }
        else{
            menuService.setMenuItems(menuService.getCustomerMainMenuItems());
        }
    }

    private void enterListMovies() {
        ioService.write(movieService.listMovies());
        if(userService.getCurrentUser() != null){
            menuService.setMenuItems(menuService.getMovieSubMenuItems());
        }
        else
        {
            menuService.setMenuItems(menuService.getUnLoginMainMenuItems());
        }
    }

    private void enterListBooks() {
        ioService.write(bookService.listBooks());
        if(userService.getCurrentUser() != null){
            menuService.setMenuItems(menuService.getBookSubMenuItems());
        }
        else
        {
            menuService.setMenuItems(menuService.getUnLoginMainMenuItems());
        }
    }
}
